require 'rubygems'
require 'test/unit'
require 'mocha'
require 'server_interface'
require 'database'

class TestServerInterface < Test::Unit::TestCase
  def setup
    @hostname = 'testing_agent'
    Database.test_connect
    @server = ServerInterface.new(stub_everything())
    @server.register(@hostname, '1234')
    @shared_file = stub_everything()
  end

  def test_tags_populate_when_we_publish
    @shared_file.expects(:tags).at_least_once.returns(['foo','bar'])
    @server.publish(@shared_file, @hostname)
    assert_equal("foo", Tag.find_by_name('foo').name)
  end

  def test_remote_file_populates_when_we_publish
    @shared_file.expects(:path).returns('foo')
    @server.publish(@shared_file, @hostname)
    assert_equal("foo", RemoteFile.find_by_path('foo').path)
  end

  def test_file_without_tags_works
    begin
      @server.publish(@shared_file, @hostname)
    rescue
      fail
    end
  end

  def test_remote_agent_created_when_we_register
    @server.register('foo','bar')
    assert_equal('foo', RemoteAgent.find_by_port('bar').host)
  end

  def test_file_associated_with_agent_when_published
    @shared_file.expects(:path).returns("foo")
    @server.publish(@shared_file, @hostname)
    assert(!RemoteFile.find_by_path("foo").remote_agent.nil?)
  end

  def test_we_cant_publish_if_host_isnt_registered
    begin
      @server.publish(@shared_file, 'unknown')
      fail()
    rescue
    end
  end

end
